﻿package com.moonshineproject.plugin.actionscript.as3project.vo 
{
	import com.moonshineproject.utils.Utils;
	import flash.filesystem.File;
	
	public class BuildOptions 
	{
		public static var defaultOptions:BuildOptions = new BuildOptions();
		
		public var accessible:Boolean = false;
		public var allowSourcePathOverlap:Boolean = false;
		public var benchmark:Boolean = false;
		public var es:Boolean = false;
		public var locale:String;
		public var loadConfig:String;
		public var optimize:Boolean = true;
		public var showActionScriptWarnings:Boolean = true;
		public var showBindingWarnings:Boolean = true;
		public var showDeprecationWarnings:Boolean = true;
		public var showUnusedTypeSelectorWarnings:Boolean = true;
		public var strict:Boolean = true;
		public var useNetwork:Boolean = true;
		public var useResourceBundleMetadata:Boolean = true;
		public var warnings:Boolean = true;
		public var verboseStackTraces:Boolean = false;
		public var linkReport:String;
		public var loadExterns:String;
		public var staticLinkRSL:Boolean = false;
		public var additional:String;
		public var compilerConstants:String;
		
		public var customSDKPath:String;
		public function get customSDK():File
		{ 
			if (customSDKPath) 
			{
				var toRet:File = new File(customSDKPath);
				toRet.canonicalize();
				return toRet;
			}
			return null;
		}
		
		/**
		 * @return mxmlc arguments with defaults removed
		 */
		public function getArguments():String {
			var pairs:Object = getArgumentPairs();
			var dpairs:Object = defaultOptions.getArgumentPairs();
			var args:String = "";
			for (var p:String in pairs) {
				if (pairs[p] != dpairs[p]) {
					args += " -"+p+"="+pairs[p];
				}
			}
			if (additional) args += " "+additional.replace("\n", " ")
			return args;
		}
		private function getArgumentPairs():Object {
			return {
				"load-config+"							:	loadConfig,
				"accessible"							:	accessible,
				"allow-source-path-overlap"				:	allowSourcePathOverlap,
				"benchmark"								:	benchmark,
				"es"									:	es,
				"as3"									:	!es,
				"optimize"								:	optimize,
				"show-actionscript-warnings"			:	showActionScriptWarnings,
				"show-binding-warnings"					:	showBindingWarnings,
				"show-deprecation-warnings"				:	showDeprecationWarnings,
				"show-unused-type-selector-warnings"	:	showUnusedTypeSelectorWarnings,
				"strict"								:	strict,
				"use-network"							:	useNetwork,
				"use-resource-bundle-metadata"			:	useResourceBundleMetadata,
				"warnings"								:	warnings,
				"verbose-stacktraces"					:	verboseStackTraces,
				"link-report"							:	linkReport,
				"static-link-runtime-shared-libraries"	:	staticLinkRSL
			}
		}
		
		public function parse(build:XMLList):void 
		{
			var options:XMLList = build.option;
			
			accessible							= Utils.deserializeBoolean(options.@accessible);
			allowSourcePathOverlap				= Utils.deserializeBoolean(options.@allowSourcePathOverlap);
			benchmark							= Utils.deserializeBoolean(options.@benchmark);
			es									= Utils.deserializeBoolean(options.@es);
			optimize							= Utils.deserializeBoolean(options.@optimize);
			showActionScriptWarnings			= Utils.deserializeBoolean(options.@showActionScriptWarnings);
			showBindingWarnings					= Utils.deserializeBoolean(options.@showBindingWarnings);
			showDeprecationWarnings				= Utils.deserializeBoolean(options.@showDeprecationWarnings);
			showUnusedTypeSelectorWarnings		= Utils.deserializeBoolean(options.@showUnusedTypeSelectorWarnings);
			strict								= Utils.deserializeBoolean(options.@strict);
			useNetwork							= Utils.deserializeBoolean(options.@useNetwork);
			useResourceBundleMetadata			= Utils.deserializeBoolean(options.@useResourceBundleMetadata);
			warnings							= Utils.deserializeBoolean(options.@warnings);
			verboseStackTraces					= Utils.deserializeBoolean(options.@verboseStackTraces);
			staticLinkRSL						= Utils.deserializeBoolean(options.@staticLinkRSL);
			
			locale								= Utils.deserializeString(options.@locale);
			loadConfig							= Utils.deserializeString(options.@loadConfig);
			linkReport							= Utils.deserializeString(options.@linkReport);
			additional							= Utils.deserializeString(options.@additional);
			compilerConstants					= Utils.deserializeString(options.@compilerConstants);
			customSDKPath						= Utils.deserializeString(options.@customSDK);
		}
		
		public function toXML():XML
		{
			var build:XML = <build/>;
			
			var pairs:Object = {
				accessible							:	Utils.serializeBoolean(accessible),
				allowSourcePathOverlap				:	Utils.serializeBoolean(allowSourcePathOverlap),
				benchmark							:	Utils.serializeBoolean(benchmark),
				es									:	Utils.serializeBoolean(es),
				optimize							:	Utils.serializeBoolean(optimize),
				showActionScriptWarnings			:	Utils.serializeBoolean(showActionScriptWarnings),
				showBindingWarnings					:	Utils.serializeBoolean(showBindingWarnings),
				showDeprecationWarnings				:	Utils.serializeBoolean(showDeprecationWarnings),
				showUnusedTypeSelectorWarnings		:	Utils.serializeBoolean(showUnusedTypeSelectorWarnings),
				strict								:	Utils.serializeBoolean(strict),
				useNetwork							:	Utils.serializeBoolean(useNetwork),
				useResourceBundleMetadata			:	Utils.serializeBoolean(useResourceBundleMetadata),
				warnings							:	Utils.serializeBoolean(warnings),
				verboseStackTraces					:	Utils.serializeBoolean(verboseStackTraces),
				staticLinkRSL						:	Utils.serializeBoolean(staticLinkRSL),

				locale								:	Utils.serializeString(locale),
				loadConfig							:	Utils.serializeString(loadConfig),
				linkReport							:	Utils.serializeString(linkReport),
				additional							:	Utils.serializeString(additional),
				compilerConstants					:	Utils.serializeString(compilerConstants),
				customSDK							:	Utils.serializeString(customSDKPath)
			}
			
			build.appendChild(Utils.serializePairs(pairs, <option/>));
			
			return build;
		}
		
	}
}